library(here)

library(data.table)
library(ggplot2)
library(cowplot)

# ICC/ISDS Cases over time ------------------------------------------------

ICCcases <- data.table(readxl::read_xlsx(here('data', 'ICC Cases.xlsx')))

# REMOVE TREATY-BASED ISDS CASES
ICCcases$iccCases[ICCcases$year == 2009] <- ICCcases$iccCases[ICCcases$year == 2009] - 1 
ICCcases$iccCases[ICCcases$year == 2013] <- ICCcases$iccCases[ICCcases$year == 2013] - 1 
ICCcases$iccCases[ICCcases$year == 2014] <- ICCcases$iccCases[ICCcases$year == 2014] - 1 

ICCcases <- melt(ICCcases, id.vars = c('year'), measure.vars = colnames(ICCcases)[2:6], 
                 variable.name = 'institution', value.name = 'cases')

ggplot(subset(ICCcases, institution != 'iccCases' &
                institution != 'wtoCases' & 
                institution != 'icsidCases' & 
                year >= 1995), 
       aes(x = year, y = cases, fill = institution)) +
  geom_bar(stat = 'identity', position = 'dodge') +
  scale_y_continuous(expand = expansion(mult = c(0, 0.03))) +
  scale_x_continuous(breaks = seq(1990, 2020, 5)) +
  labs(x = '', y = 'Cases w/ State Parties') +
  scale_fill_manual(values = c('darkgrey', 'black'), 
                    labels = c("Contract-based arbitration (ICA)", 'Treaty-based arbitration (ISDS)')) +
  theme_cowplot(font_size = 14) +
  theme(legend.position = 'bottom',
        legend.title = element_blank(),
        panel.grid.major.y = element_line(linetype = "dotted"))

ggsave(here('output', 'figures', 'figure-1.pdf'),
       height = 3, width = 7, units = 'in')
